<?php
 
if ( !defined( 'MEDIAWIKI' ) ) {
    die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
 
$wgExtensionFunctions[] = 'wfSetupNescaUtils';
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'NescaUtils',
);
 
$wgHooks['LanguageGetMagic'][]       = 'wfNescaUtilsLanguageGetMagic';
 
class ExtNescaUtils {
    var $mSort = array();
 
    function addtostack( &$parser, $expr = '' ) {
	if (mb_strlen($expr)>0){
		array_push($this->mSort,$expr);
	}
	return '';
    }

    function getsortedstack( &$parser, $separator=".") {
	sort($this->mSort,SORT_STRING);
	array_unique($this->mSort);
	$result="";
	foreach($this->mSort as $element) {
		$result.=$element.$separator;
	}
	$this->mSort = array();
	return $result;
    }
 
 
    function to24time( &$parser, $expr) {
	if (mb_strlen($expr)==0){
		return '00:00';
	}
	$expr=mb_ereg_replace("[^0123456789\:]","",$expr);
	$hour = $expr;
	$minute = '00';
	if (($array = mb_split(":",$expr)) && count($array)>0) {
		$hour = $array[0];
		if (count($array)>1) {
			$minute = $array[1];
		} 
	}
	
	$len = mb_strlen($hour);
	if ($len==1) {
		$hour = '0'.$hour;
	} else if ($len > 2) {
		return '00:00';
	}
		 
	$len = mb_strlen($minute);
	if ($len==1) {
		$minute = '0'.$minute;
	} else if ($len > 2) {
		return '00:00';
	}
	
	$result = "$hour:$minute";

	if ($result=="24:00")
		return $result;

	if (!strtotime($result)) {
		return '00:00';
	}

	return $result;
    }

    function spam( &$parser, $to_array, $topic, $message ) {
$arr=explode(",",$to_array);	
	foreach($arr as $mail) {
		mail($mail,$topic,$message);
	}
	//return UserMailer::send("braelis@gmail.com", "noreplay@hopesland.com", 'topic', 'message', "noreplay@hopesland.com");
    }

    function username( &$parser) {
	global $wgUser;
	return $wgUser->getName();
    }
 
}
 
function wfSetupNescaUtils() {
    global $wgParser, $wgExtNescaUtils, $wgHooks;
 
    $wgExtNescaUtils = new ExtNescaUtils;
 
    $wgParser->setFunctionHook( 'addtostack', array( &$wgExtNescaUtils, 'addtostack' ) );
    $wgParser->setFunctionHook( 'getsortedstack', array( &$wgExtNescaUtils, 'getsortedstack' ) );

    $wgParser->setFunctionHook( 'spam', array( &$wgExtNescaUtils, 'spam' ) );

    $wgParser->setFunctionHook( 'username', array( &$wgExtNescaUtils, 'username' ) );
    $wgParser->setFunctionHook( 'to24time', array( &$wgExtNescaUtils, 'to24time' ) );
}
 
function wfNescaUtilsLanguageGetMagic( &$magicWords, $langCode = 0 ) {
        require_once( dirname( __FILE__ ) . '/NescaUtils.i18n.php' );
        foreach( efNescaUtilsWords( $langCode ) as $word => $trans )
            $magicWords[$word] = $trans;
        return true;
}
